// 登录的路由
const express = require("express");
let router = express.Router();
// 引入用户数据库模型
const userModel = require("../models/usermodel.js");
// // 引入 bodyparser
// const bodyParser = require("body-parser")
// 引入加密数据的模块  md5
const md5 = require("md5");
// 签发token 模块
const jwt = require("jsonwebtoken");
router.post("/checkuser", async (req, res) => {
    let user = req.body
    let username = user.username;
    let pwd = user.pwd
    console.log(username, pwd);
    let basedata = await userModel.findOne({ username: username, pwd: md5(pwd) });
    console.log(basedata);

    if (basedata !== null) {
        // 用户名密码正确

        // token 参数         1:传递参数值，给前端  2：token密码  3：token 过期时间
        let token = jwt.sign({ myvalue: 123 }, "mytoken", { expiresIn: "2h" })
        res.json({
            info: "请求成功",
            status: 1,
            token
        })
    } else {
        res.json({
            info: "请求失败",
            status: 0
        })
    }

})

module.exports = router;